#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
//定义一个全局变量
int gval = 0;

int main()
{
    printf("I am a process, pid:%d, ppid:%d\n", getpid(), getppid());

    pid_t id = fork();
    if (id > 0) 
    {
        //此为创建成功的子进程PID
        //因为给父进程返回子进程的代码
        //所以这里执行父进程的代码
        while(1)
        {

            printf("我是父进程, pid:%d, ppid:%d, ret id:%d, gval:%d\n",getpid(), getppid(), id, gval);
            //父进程只读
            sleep(1);
        }
    }
    else if(id == 0)
    {

        while(1)
        {
            //这里给子进程返回0
            //所以执行的是子进程的代码
            printf("我是子进程, pid:%d, ppid:%d, ret id:%d, gval:%d\n",getpid(), getppid(), id, gval);
            //子进程修改
            gval++;
            sleep(1);
        }
    }
    printf("\n");
    return 0;
}



    //(void)id;

    //printf("I am a 分支！pid:%d, ppid:%d\n", getpid(), getppid());
    //sleep(1);


    //chdir("/home/gan/");
    //FILE* fp = fopen("log.txt", "w");
    //if (fp == NULL) {

    //}
    //获取父进程pid
    //pid_t fid = getppid();
    //pid_t id = getpid();

    //while(1)
    //{
    //    printf("hello bit, I am a process, pid:%d, ppid:%d\n", id, fid);
    //    //每隔一秒打印一次
    //    sleep(1);
    //}
